let app = getApp();
Page({
  data: {
    current: 0,
    navFlag: '1',
    openid: null,
    orderlist: [],
    unpaidOrder: [],
    UserInfo:[]
  },

  onPageScroll(e) {
    let nvaStyle = this.data.navFlag;
    nvaStyle = e.scrollTop > 100 ? this.data.navFlag = '1' : this.data.navFlag = '2';
    this.setData({
      navFlag: nvaStyle
    })
  },
  onPullDownRefresh() {
    wx.showLoading();
    let index = this.data.current;
    if (index === 0) {
      this.ajaxOrder('');
    } else if (index == 1) {
      this.ajaxOrder('zhidan');
    } else if (index == 2) {
      this.ajaxOrder('zhifu');
    } else if (index == 3) {
      this.ajaxOrder('yifahuo');
    }
  },
  stopPullDownRefresh() {
    wx.stopPullDownRefresh({
      complete(res) {
        wx.hideLoading();
      }
    })
  },
  onLoad(options) {
    this.loadInfo();
    let that = this,
      index = options.current;
    wx.getStorage({
      key: 'openid',
      success(res) {
        that.setData({
          openid: res.data,
          current: index
        });
        if (index == 0) {
          that.ajaxOrder('');
        } else if (index == 1) {
          that.ajaxOrder('zhidan');
        } else if (index == 2) {
          that.ajaxOrder('zhifu');
        } else if (index == 3) {
          that.ajaxOrder('yifahuo');
        }
      }
    });
  },
  loadInfo() {
    let that = this;
    wx.getUserInfo({
      lang:'zh_CN',
      success(e) {
        let userInfo = e.userInfo;
       that.setData({
         userInfo: userInfo
          });
        app.get('login', {
          action: 'loginUserInfo1',
          openid: that.data.openid,
          WeiXinTouXiang: userInfo.avatarUrl,
          ChengShi: userInfo.country + userInfo.city,
          NiCheng: userInfo.nickName
        }).then((res) => {});
      }
    })
  },

  eval(e) {
    let userInfo = e.detail.userInfo;
     wx.setStorage({
      key: 'userinfo',
       data: JSON.stringify(userInfo),
      success(res) {
       wx.navigateTo({
            url: "/pages/eval/eval?id=" + e.currentTarget.id + '&name=' + e.currentTarget.dataset.name
          })
      }
    })
     
  
  },
  ajaxOrder(state) {
    let that = this;
    app.get('DingDanDiZhi', {
      action: 'getDingDanByKeHu',
      openid: this.data.openid,
      dingdanzhuangtai: state,
      dtstart: '',
      dtend: '',
      yema: 1,
    }).then((res) => {
      wx.hideLoading();
      that.stopPullDownRefresh();
      that.setData({
        orderlist: res.data
      })
      if (res.data.length < 1) {
        app.toast('暂未查找到数据')
      }
    })
  },
  payOrder(e) {
    wx.showLoading();
    let orderid = e.target.dataset;
    let that = this;
    //统一支付 
    app.get('XiaDan', {
      totalfee: Number(orderid.price) * 100,
      openid: that.data.openid,
      dingdanhao: orderid.ordercode
    }).then((res) => {
      let pay = res;
      console.log(pay)
      //发起支付      
      let timeStamp = pay.timeStamp,
        packages = pay.package,
        paySign = pay.paySign,
        nonceStr = pay.nonceStr,
        param = {
          "timeStamp": timeStamp,
          "package": packages,
          "paySign": paySign,
          "signType": "MD5",
          "nonceStr": nonceStr
        };
      wx.hideLoading();
      that.pay(param);
    });
  },
  pay(param) {
    wx.requestPayment({
      timeStamp: param.timeStamp,
      nonceStr: param.nonceStr,
      package: param.package,
      signType: param.signType,
      paySign: param.paySign,
      success(res) {
        // success 
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        });
        setTimeout(() => {
          wx.redirectTo({
            url: '/pages/myOrder/myOrder?flag=1',
          });
        }, 2000);
      },
      fail: function(res) {
        console.log(res)
        wx.showToast({
          title: '支付失败',
          duration: 2000
        });
      }
    })
  },
  deleteOrder(e) {
    let orderCode = e.target.dataset.ordercode,
      that = this;
    app.get('DingDanDiZhi', {
      action: 'quxiao',
      ID: orderCode
    }).then((res) => {
      app.toast(res.msg);
      that.ajaxOrder();
    })
  },
  switchSlider(e) {
    this.setData({
      current: e.target.dataset.index
    });
    let index = e.currentTarget.dataset.index;
    if (index == 0) {
      this.ajaxOrder('');
    } else if (index == 1) {
      this.ajaxOrder('zhidan');
    } else if (index == 2) {
      this.ajaxOrder('zhifu');
    } else if (index == 3) {
      this.ajaxOrder('yifahuo');
    }
  },
  changeSlider(e) {
    this.setData({
      current: e.detail.current
    });
  },
  viewDetails(e) {
    wx.navigateTo({
      url: '/pages/myOrderDetail/myOrderDetail?id=' + e.currentTarget.id,
    });
  }
})